define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'acctmoneylog/index' + location.search,
                    add_url: 'acctmoneylog/add',
                    edit_url: 'acctmoneylog/edit',
                    del_url: 'acctmoneylog/del',
                    multi_url: 'acctmoneylog/multi',
                    import_url: 'acctmoneylog/import',
                    table: 'acct_money_log',
                }
            });

            var table = $("#table");
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log(data);
                //这里我们手动设置底部的值
                $("#quota").text(data.extend.quota);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    var ids = Fast.api.query('ids');
                    if (ids) {
                        filter.user_id = Fast.api.query('ids');
                        op.user_id = "=";
                        params.filter = JSON.stringify(filter);
                        params.op = JSON.stringify(op);
                    }
                    return params;
                },
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        // { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'source', title: __('订单编号'), operate: 'Like' },
                        { field: 'money', title: __('Money'), operate: 'BETWEEN' },
                        { field: 'before', title: __('Before'), operate: 'BETWEEN' },
                        { field: 'after', title: __('After'), operate: 'BETWEEN' },
                        { field: 'memo', title: __('备注'), operate: 'Like' },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
